package com.my.trie;

import com.my.model.HandlerDetails;

import java.util.ArrayList;
import java.util.List;

/**
 * @author WangLuoXin
 * &#064;Date 2024/9/13  13:35
 **/
public class TreeNode {
    //节点名称
    private String name;

    //下级节点
    private List<TreeNode> children;

    //是否为尾节点
    private Boolean isEnd = Boolean.FALSE;

    private HandlerDetails handlerDetails;


    public TreeNode(String name, List<TreeNode> children) {
        this.name = name;
        this.children = children;
    }

//    public void setEnd(Boolean end) {
//        isEnd = end;
//    }

    public void putPath(String path, HandlerDetails handlerDetails) {
        TreeNode parent = this;
        String[] paths = path.split("/");
        for (int i = 0; i < paths.length; i++) {
            String name = paths[i];
            if (i == 0) {
                continue;
            }

            boolean isMatch = false;
            List<TreeNode> newChildrenList = parent.children;
            for (TreeNode treeNode : newChildrenList) {
                if (treeNode.name.equals(name)) {
                    isMatch = true;
                    parent = treeNode;
                    if (i == paths.length - 1) {
                        parent.isEnd = Boolean.TRUE;
                        parent.handlerDetails = handlerDetails;
                    }
                    break;
                }
            }
            if (!isMatch) {
                TreeNode node;
                node = new TreeNode(name, new ArrayList<>());
                if (i == paths.length - 1) {
                    node.isEnd = Boolean.TRUE;
                    node.handlerDetails = handlerDetails;
                }
                newChildrenList.add(node);
                parent.children = newChildrenList;
                parent = node;
            }
        }
    }

    public TreeNode getPath(String path) {
        TreeNode parent = this;
        String[] paths = path.split("/");
        for (int i = 0; i < paths.length; i++) {
            if (i == 0) {
                continue;
            }
            List<TreeNode> newChildrenList = parent.children;
            for (TreeNode treeNode : newChildrenList) {
                if (treeNode.name.equals(paths[i]) || (treeNode.name.startsWith("{") && treeNode.name.endsWith("}"))) {
                    parent = treeNode;
                    if (i == paths.length - 1) {
                        if (parent.isEnd) {
                            return parent;
                        }
                        return null;
                    }
                    break;
                }
            }
        }
        return null;
    }


    @Override
    public String toString() {
        return "TreeNode{" +
                "name='" + name + '\'' +
                ", children=" + children +
                ", isEnd=" + isEnd +
                '}';
    }

    public HandlerDetails getHandlerDetails() {
        return handlerDetails;
    }

    public Boolean getEnd() {
        return isEnd;
    }
}
